<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use App\Models\product\ProductModel;
use App\Models\product\ProductSonModel;
use App\Models\users\UsersModel;
use App\Models\users\UsersNewModel;
use App\Service\admin\ProductService;
use App\Service\product\OrderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class OrderController extends Controller
{

    /**
     * 更新订单状态
     */
    public function updateOrderStatus(Request $request): JsonResponse
    {
        $data = $request->data;

        $validator = Validator::make($data, [
            'mobile' => ['required'], // 拉新手机号
            'pro' => ['required'], // 商品id
            'order_sn' => ['required'], // 订单号
            'order_status' => ['in:0,1,2,3,4'], // 0待支付，1已支付，2已完成，3无效订单，4已退款
            'pay_price' => ['required', 'min:0'], // 支付金额
            'pay_time' => ['required', function ($attribute, $value, $fail) {
                if ( $value > 0 && !Tools::isTimestamp($value) ) {
                    $fail('支付时间格式不是有效的时间戳');
                }
            }],
            'create_time' => ['required'],
            'update_time' => ['required', function ($attribute, $value, $fail) {
                if ( !Tools::isTimestamp($value) ) {
                    $fail('更新时间格式不是有效的时间戳');
                }
            }],

        ], [
            'mobile.required' => '请填写拉新手机号',
            'pro.required' => '请选择商品',
            'order_sn.required' => '请填写订单号',
            'order_status.in' => '订单状态有误',
            'pay_price.required' => '请输入支付金额',
            'pay_price.min' => '支付金额不能小于0',
            'pay_time.required' => '请输入支付时间',
            'create_time.required' => '请输入下单时间',
            'update_time.required' => '请输入更新时间',
        ]);

        if ($validator->fails()) {
            return ApiResponse::apiResponseData(400, $validator->errors()->first());
        }

        // 检查拉新用户
        $newInfo = UsersNewModel::where('mobile', $data['mobile'])->first();
        if (!$newInfo) {
            return ApiResponse::apiResponseData(400, '拉新用户不存在');
        }
        // 检查用户
        $userInfo = UsersModel::where('id', $newInfo->uid)->first();
        if (!$userInfo) {
            return ApiResponse::apiResponseData(400, '用户不存在');
        }
        // 检查商品
        $productInfo = ProductSonModel::with(['product'])->where('id', $data['pro'])->first();
        if (!$productInfo) {
            return ApiResponse::apiResponseData(400, '商品不存在');
        }

        $data['new_id'] = $newInfo->id;
        $data['uid'] = $userInfo->id;
        $data['product_id'] = $productInfo->id;
        $data['order_commission_ratio'] = ProductService::instance()->getProductCommissionRatio($productInfo->product->commission_ratio, $productInfo->product->belong_id, $productInfo->product->class_id);
        $data['commission'] = $data['order_commission_ratio'] * $data['pay_price'];

        $result = OrderService::instance()->updateOrderStatus($data);
        if ($result['code'] == 200) {
            return ApiResponse::apiResponseData(200, $result['message']);
        }
        return ApiResponse::apiResponseData(400, $result['message']);
    }


}
